package com.example.springboot.mapper;
import org.apache.ibatis.annotations.*;
import java.util.List;
import com.example.springboot.pojo.Orders;
import com.example.springboot.pojo.User;
import com.example.springboot.pojo.Goods;
@Mapper
public interface OrdersMapper {
    @Insert("insert into orders(orders_number,goods_id,user_id,goods_number,create_time,price) " +
            "values(#{ordersNumber},#{goodsId},#{userId},#{goodsNumber},now(),#{price})")
    void create(Orders orders);
    @Delete("delete from orders where id=#{id}")
    void delete(Integer id);
    @Select({
            "SELECT o.id, o.orders_number , o.goods_id, g.photo AS goodsPhoto , g.name AS goodsName, o.user_id, u.username AS username, u.user_phone AS userPhone, u.user_address AS userAddress, o.goods_number, o.create_time, o.price",
            "FROM orders o",
            "LEFT JOIN goods g ON o.goods_id = g.id",
            "LEFT JOIN user u ON o.user_id = u.id"
    })
    List<Orders> findAll();
    @Select("select * from orders where id=#{id}")
    Orders findById(Integer id);
    @Select({
            "SELECT o.id, o.orders_number, o.goods_id, g.photo AS goodsPhoto, g.name AS goodsName, o.user_id, u.username AS username, u.user_phone AS userPhone, u.user_address AS userAddress, o.goods_number, o.create_time, o.price",
            "FROM orders o",
            "LEFT JOIN goods g ON o.goods_id = g.id",
            "LEFT JOIN user u ON o.user_id = u.id",
            "WHERE o.user_id= #{userId}"
    })
    List<Orders> findByUserId(Integer userId);
    @Update("update orders set orders_number=#{ordersNumber},goods_id=#{goodsid},user_id=#{userId}," +
            "goods_number=#{goodsNumber},create_time=now(),price=#{price} where id=#{id}")
    void update(Orders orders);

    //根据订单编号
    @Select({
            "SELECT o.id, o.orders_number, o.goods_id, g.photo AS goodsPhoto, g.name AS goodsName, o.user_id, u.username AS username, u.user_phone AS userPhone, u.user_address AS userAddress, o.goods_number, o.create_time, o.price",
            "FROM orders o",
            "LEFT JOIN goods g ON o.goods_id = g.id",
            "LEFT JOIN user u ON o.user_id = u.id",
            "WHERE o.orders_number= #{ordersNumber}"
    })
    Orders findByOrdersNumber(String ordersNumber);
}
